package leetcode2;
import java.util.*;

public class Day14 {






    public int[] sortArrayByParity(int[] nums) {
        int n = nums.length;
        int left=0,right=n-1;
        while(left<right) {
            if(nums[left]%2==0) {
                left++;
            }else {
                int tmp = nums[left];
                nums[left]=nums[right];
                nums[right]=tmp;
                right--;
            }

        }
        return nums;
    }
    public int totalFruit(int[] fruits) {
        Map<Integer,Integer> map = new HashMap<>();//存放水果的种类
        int ret=0;
        for(int left=0,right=0;right<fruits.length;right++) {
            //1.进窗口
            int tmp = fruits[right];
            if(map.get(tmp)==null) { //1.第一次存入
                map.put(tmp,1);
            }else {
                map.put(tmp,map.get(tmp)+1);//2.在后续加入
            }
            //2.出窗口
            while(map.size()>2) {
                //说明种类超出两种，需要出窗口
                tmp = fruits[left];
                if(map.get(tmp) == 1) {
                    map.remove(tmp);
                }else {
                    map.put(tmp,map.get(tmp)-1);
                }
                left++;
            }
            if(map.size() <= 2) {
                ret = Math.max(ret,right-left+1);
            }
        }
        return ret;
    }
}
